﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;

namespace Batman.Common.Extensions
{
    public static class ApplicationBuilderExtension
    {
        [Obsolete("请使用Batman.Cms.Core.Extensions.ApplicationBuilderExtension.UseBatmanMvcAuthentication")]
        public static void UseAuthenticationWithConfig(this IApplicationBuilder app, IHostingEnvironment env)
        {
            var authenticationInfo = env.GetAuthenticationInfo();
            var authorizationHost = env.GetAuthorizationHost();

            app.Use(async (context, next) =>
            {
                HostString hostString = new HostString(authorizationHost.Host, authorizationHost.Port);
                context.Request.Host = hostString;

                await next.Invoke();
            });

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(name: "default", template: "/" + authenticationInfo.ModuleName + "/{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
